package com.kvest.mobile_newspaper.android;

import java.util.ArrayList;

import com.kvest.mobile_newspaper.R;
import com.kvest.mobile_newspaper.android.ClubListAdapter.ActionsListener;
import com.kvest.mobile_newspaper.datamodel.Element;
import com.kvest.mobile_newspaper.datamodel.ElementsContainer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MainMenuListAdapter extends ArrayAdapter<Element>
{
	private ArrayList<Element> items;
	private LayoutInflater inflater;

	private ActionsListener actionsListener;
	private OnClickListener onClickListener;
	
    public MainMenuListAdapter(Context context, int textViewResourceId, ArrayList<Element> items) 
    {
        super(context, textViewResourceId, items);
        this.inflater = LayoutInflater.from(context);
        this.items = items;
        
        onClickListener = new OnClickListener() 
    	{	
			public void onClick(View v) 
			{
				if (actionsListener != null)
				{
					actionsListener.elementAction((Element)v.getTag());
				}					
			}
		};
        actionsListener = null;
    }

    public void setaAtionsListener(ActionsListener actionsListener)
	{
		this.actionsListener = actionsListener;
	}
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View view = convertView;
        if (view == null) 
        {
        	view = this.inflater.inflate(R.layout.main_menu_content_item, null);
        }
        Element element = items.get(position);
        TextView name = (TextView) view.findViewById(R.id.main_menu_item_text);
        TextView unreadCount = (TextView) view.findViewById(R.id.main_menu_item_unread_count);
        if (element != null && name != null) 
        {
        	name.setText(element.getName());
        }
        if (element != null && unreadCount != null) 
        {
        	unreadCount.setText("(" + Integer.toString(element.getUnreadArticlesCount()) + ")");
        }
        
        ImageView iv = (ImageView)view.findViewById(R.id.iv_action);
        if (iv != null)
        {
        	iv.setTag(element);
        	iv.setOnClickListener(onClickListener);
        }
        
        return view;
    }
	
    
    public interface ActionsListener
	{
		public void elementAction(Element element);
	}
}
